package com.example.jones.function;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.example.jones.test.MainActivity;
import com.example.jones.test.R;

@SuppressLint("NewApi")
public class AlarmService extends Service {
    private NotificationManager nm;
    private Intent it;
    private PendingIntent pi;
    private Notification notification;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        nm = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);

        it = new Intent(this, MainActivity.class);
        it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);

        pi = PendingIntent.getActivity(this, 0, it,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // notification = new Notification.Builder(this).setAutoCancel(true)
        // .setTicker("语音菜谱有新菜单了，赶快来看看吧！").setContentTitle("新菜单提醒")
        // .setContentText("语音菜谱的菜单已经更新啦，走过路过不要错过！")
        // .setDefaults(Notification.DEFAULT_ALL)
        // .setWhen(System.currentTimeMillis()).setContentIntent(pi)
        // .build();

        notification = new Notification(R.drawable.logo, "语音菜谱有新菜单了，赶快来看看吧！",
                System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_NO_CLEAR;
        notification.defaults |= Notification.DEFAULT_ALL;

        notification.setLatestEventInfo(this, "新菜单提醒",
                "语音菜谱的菜单已经更新啦，不要错过哦！", pi);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        nm.notify(0, notification);
        return START_STICKY;
    }
}
